package matlap.client.view.admin;

import java.util.Iterator;
import java.util.List;

import matlap.client.services.student.StudentService;
import matlap.client.services.student.StudentServiceAsync;
import matlap.shared.Score;
import matlap.shared.Student;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.DecoratorPanel;

public class StudentsScoreComposite extends Composite {

	StudentServiceAsync r1 = GWT.create(StudentService.class);
	private Grid g2;
	private VerticalPanel vp;
	private Label lbName;
	private TextBox tbName;
	private Button btnSearch;
	protected String name;

	Panel newPanel = new HorizontalPanel();
	Panel updatePanel = new VerticalPanel();
	Panel buttonPanel = new HorizontalPanel();
	HorizontalPanel simplePanel = new HorizontalPanel();
	Panel gridPanel = new HorizontalPanel();

	Button newButton;
	Button updateButton;

	public StudentsScoreComposite() {

		init();
		initGrid();

	}

	private void init() {
		vp = new VerticalPanel();
		initWidget(vp);
		vp.setHeight("500px");

		initButtonPanel();
		vp.add(buttonPanel);
		updateButtonColor(1, 0);

		initNewPanel();
		initUpdatePanel();
		simplePanel.setBorderWidth(1);
		simplePanel.add(newPanel);
		vp.add(simplePanel);
		simplePanel.setWidth("687px");

		DecoratorPanel decoratorPanel = new DecoratorPanel();
		vp.add(decoratorPanel);

		Label lblNewLabel_2 = new Label("Eddigi pontsz\u00E1mok");
		lblNewLabel_2.setStyleName("BigBold");
		decoratorPanel.setWidget(lblNewLabel_2);
		
		VerticalPanel verticalPanel = new VerticalPanel();
		verticalPanel.setBorderWidth(1);
		vp.add(verticalPanel);
		verticalPanel.setWidth("687");

		FlexTable filterFlex = new FlexTable();
		verticalPanel.add(filterFlex);
		filterFlex.setSize("683px", "57px");

		lbName = new Label("N\u00E9v");
		filterFlex.setWidget(0, 0, lbName);

		tbName = new TextBox();
		filterFlex.setWidget(1, 0, tbName);

		btnSearch = new Button("New button");
		btnSearch.setText("Keres");
		filterFlex.setWidget(1, 1, btnSearch);
		filterFlex.getCellFormatter().setHorizontalAlignment(1, 1, HasHorizontalAlignment.ALIGN_RIGHT);

		btnSearch.addClickHandler(new ClickHandler() {

			public void onClick(ClickEvent event) {
				name = tbName.getText();

				r1.getStudentsBy(name, "", "", "",
						new AsyncCallback<List<Student>>() {

							@Override
							public void onFailure(Throwable caught) {
								// TODO Auto-generated method stub

							}

							@Override
							public void onSuccess(List<Student> result) {
								int i = 1;
								Iterator<Student> it = result.iterator();
								g2.resize(result.size() + 1, 12);
								while (it.hasNext()) {
									Student stu = (Student) it.next();
									g2.setText(i, 0, stu.getLastName() + " "
											+ stu.getFirstName());
									g2.setText(i, 1, stu.getUserName());
									for (int j = 2; j < 12; j++) {
										g2.setText(i, j,
												" " + stu.getScores(j - 2));
									}
									i++;

								}

							}
						});
			}
		});

	}

	public void updateButtonColor(int c1, int c2) {
		if (c1 == 0) {
			newButton.setStyleName("Button1");
		} else {
			newButton.setStyleName("Button2");
		}
		if (c2 == 0) {
			updateButton.setStyleName("Button1");
		} else {
			updateButton.setStyleName("Button2");
		}

	}

	public void initGrid() {
		g2 = new Grid(2, 12);
		g2.setBorderWidth(2);
		
		Label lb0 = new Label("N\u00E9v");
		lb0.setStyleName("Header");
		g2.setWidget(0, 0, lb0);
		
		Label lb1 = new Label("Felhaszn\u00E1l\u00F3n\u00E9v");
		lb1.setStyleName("Header");
		g2.setWidget(0, 1, lb1);
		
		Label lb2 = new Label("Szeptember");
		lb2.setStyleName("Header");
		g2.setWidget(0, 2, lb2);
		
		Label lb3 = new Label("Okt\u00F3ber");
		lb3.setStyleName("Header");
		g2.setWidget(0, 3, lb3);

		Label lb4 = new Label("November");
		lb4.setStyleName("Header");
		g2.setWidget(0, 4, lb4);
		
		Label lb5 = new Label("December");
		lb5.setStyleName("Header");
		g2.setWidget(0, 5, lb5);

		Label lb6 = new Label("Janu\u00E1r");
		lb6.setStyleName("Header");
		g2.setWidget(0, 6, lb6);

		Label lb7 = new Label("Febru\u00E1r");
		lb7.setStyleName("Header");
		g2.setWidget(0, 7, lb7);

		Label lb8 = new Label("M\u00E1rcius");
		lb8.setStyleName("Header");
		g2.setWidget(0, 8, lb8);

		Label lb9 = new Label("\u00C1prilis");
		lb9.setStyleName("Header");
		g2.setWidget(0, 9, lb9);

		Label lb10 = new Label("M\u00E1jus");
		lb10.setStyleName("Header");
		g2.setWidget(0, 10, lb10);

		Label lb11 = new Label("J\u00FAnius");
		lb11.setStyleName("Header");
		g2.setWidget(0, 11, lb11);
		
		gridPanel.add(g2);
		vp.add(gridPanel);
		g2.setWidth("687px");
		r1.getStudentsNumber(new AsyncCallback<Integer>() {

			@Override
			public void onFailure(Throwable caught) {
				Window.alert("Gond van");

			}

			@Override
			public void onSuccess(Integer result) {
				// TODO Auto-generated method stub
				g2.resize(result + 1, 12);
			}
		});

		r1.getStudents(new AsyncCallback<List<Student>>() {

			@Override
			public void onFailure(Throwable caught) {
				Window.alert("Gond van stundentekkel");

			}

			@Override
			public void onSuccess(List<Student> result) {
				// TODO Auto-generated method stub
				int i = 1;
				g2.resize(result.size() + 1, 12);
				Iterator<Student> it = result.iterator();
				while (it.hasNext()) {
					Student stu = (Student) it.next();
					g2.setText(i, 0,
							stu.getLastName() + " " + stu.getFirstName());
					g2.setText(i, 1, stu.getUserName());
					for (int j = 2; j < 12; j++) {
						g2.setText(i, j, " " + stu.getScores(j - 2));
					}
					i++;

				}

			}

		});

	}

	public void initButtonPanel() {
		newButton = new Button("Pontsz\u00E1m felvezet\u00E9se");
		newButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent sender) {
				updateButtonColor(1, 0);
				simplePanel.clear();
				simplePanel.add(newPanel);
			}
		});
		buttonPanel.add(newButton);
		newButton.setWidth("341px");

		updateButton = new Button("Pontsz\u00E1m m\u00F3dos\u00EDt\u00E1sa");
		updateButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent sender) {
				updateButtonColor(0, 1);
				simplePanel.clear();
				simplePanel.add(updatePanel);
			}
		});
		buttonPanel.add(updateButton);
		updateButton.setWidth("346px");

	}

	public void initNewPanel() {
		FlexTable fl = new FlexTable();
		newPanel.add(fl);
		fl.setSize("684px", "38");

		Label vnLabel = new Label("Felhaszn\u00E1l\u00F3n\u00E9v");
		fl.setWidget(0, 0, vnLabel);
		vnLabel.setWidth("225px");
		Label cimLabel = new Label("H\u00F3nap");
		fl.setWidget(0, 1, cimLabel);

		final ListBox userBox = new ListBox();
		fl.setWidget(1, 0, userBox);
		userBox.addItem("");
		r1.getUserNames(new AsyncCallback<List<String>>() {

			public void onFailure(Throwable caught) {
				Window.alert("Adatbazis problema");

			}

			public void onSuccess(List<String> result) {
				Iterator<String> it = result.iterator();
				while (it.hasNext()) {
					userBox.addItem((String) it.next());
				}

			}

		});

		final ListBox monthBox = new ListBox();
		fl.setWidget(1, 1, monthBox);
		monthBox.addItem("");
		monthBox.addItem("Szeptember");
		monthBox.addItem("Okt\u00F3ber");
		monthBox.addItem("November");
		monthBox.addItem("December");
		monthBox.addItem("Janu\u00E1r");
		monthBox.addItem("Febru\u00E1r");
		monthBox.addItem("M\u00E1rcius");
		monthBox.addItem("\u00C1prilis");
		monthBox.addItem("M\u00E1jus");
		monthBox.addItem("J\u00FAnius");

		Label lblNewLabel_1 = new Label("Pontsz\u00E1m");
		fl.setWidget(2, 0, lblNewLabel_1);
		
		final ListBox scoreBox = new ListBox();
		fl.setWidget(3, 0, scoreBox);
		scoreBox.addItem("0");
		scoreBox.addItem("1");
		scoreBox.addItem("2");
		scoreBox.addItem("3");
		scoreBox.addItem("4");
		scoreBox.addItem("5");
		scoreBox.addItem("6");
		scoreBox.addItem("7");
		scoreBox.addItem("8");
		scoreBox.addItem("9");
		scoreBox.addItem("10");
		
		Button btnSave1 = new Button("Ment\u00E9s");
		fl.setWidget(3, 1, btnSave1);
		fl.getCellFormatter().setHorizontalAlignment(3, 1, HasHorizontalAlignment.ALIGN_RIGHT);
		btnSave1.addClickHandler(new ClickHandler() {

			public void onClick(ClickEvent event) {
				String username = userBox.getItemText(userBox
						.getSelectedIndex());
				int month_index = monthBox.getSelectedIndex();
				int score = Integer.parseInt(scoreBox.getItemText(scoreBox.getSelectedIndex()));

				if ((username.length() == 0) || (month_index == 0)) {
					Window.alert("Nem adott meg minden adatot!");
				} else {
					userBox.setSelectedIndex(0);
					monthBox.setSelectedIndex(0);
					scoreBox.setSelectedIndex(0);

					Score s = new Score(username, month_index, score);
					r1.insertStudentScore(s, new AsyncCallback<Void>() {

						@Override
						public void onFailure(Throwable caught) {
							Window.alert("hiba");

						}

						@Override
						public void onSuccess(Void result) {
							Window.alert("Sikeres");
							gridPanel.clear();
							g2.clear();
							initGrid();

						}

					});

				}
			}

		});
	}

	public void initUpdatePanel() {
		FlexTable fl2 = new FlexTable();
		updatePanel.add(fl2);
		fl2.setSize("677px", "38px");

		Label lblNewLabel = new Label("Felhaszn\u00E1l\u00F3n\u00E9v");
		fl2.setWidget(0, 0, lblNewLabel);

		Label vn2Label = new Label("H\u00F3nap");
		fl2.setWidget(0, 2, vn2Label);

		final ListBox userBox = new ListBox();
		fl2.setWidget(1, 0, userBox);
		fl2.getCellFormatter().setHorizontalAlignment(3, 2,
				HasHorizontalAlignment.ALIGN_LEFT);
		userBox.addItem("");
		r1.getUserNames(new AsyncCallback<List<String>>() {

			public void onFailure(Throwable caught) {
				Window.alert("Adatbazis problema");

			}

			public void onSuccess(List<String> result) {
				Iterator<String> it = result.iterator();
				while (it.hasNext()) {
					userBox.addItem((String) it.next());
				}

			}

		});

		final ListBox monthBox = new ListBox();
		fl2.setWidget(1, 2, monthBox);
		monthBox.addItem("");
		monthBox.addItem("Szeptember");
		monthBox.addItem("Okt\u00F3ber");
		monthBox.addItem("November");
		monthBox.addItem("December");
		monthBox.addItem("Janu\u00E1r");
		monthBox.addItem("Febru\u00E1r");
		monthBox.addItem("M\u00E1rcius");
		monthBox.addItem("\u00C1prilis");
		monthBox.addItem("M\u00E1jus");
		monthBox.addItem("J\u00FAnius");

		Label lblNewLabel_3 = new Label("Pontsz\u00E1m");
		fl2.setWidget(2, 0, lblNewLabel_3);

		final TextBox scoreBox = new TextBox();
		fl2.setWidget(3, 0, scoreBox);

		Button btnSave2 = new Button("Ment\u00E9s");
		fl2.setWidget(3, 2, btnSave2);
		btnSave2.addClickHandler(new ClickHandler() {

			public void onClick(ClickEvent event) {
				String username = userBox.getItemText(userBox
						.getSelectedIndex());
				int month_index = monthBox.getSelectedIndex();
				String sc = scoreBox.getText();

				if ((username.length() == 0) || (sc.length() == 0)
						|| (month_index == 0)) {
					Window.alert("Nem adott meg minden adatot!");
				} else {
					userBox.setSelectedIndex(0);
					monthBox.setSelectedIndex(0);
					scoreBox.setText("");
					int score = Integer.parseInt(sc);
					Score s = new Score(username, month_index, score);
					r1.updateStudentScore(s, new AsyncCallback<Void>() {

						@Override
						public void onFailure(Throwable caught) {
							Window.alert("hiba");

						}

						@Override
						public void onSuccess(Void result) {
							Window.alert("Sikeres");
							gridPanel.clear();
							g2.clear();
							initGrid();
						}

					});
				}
			}

		});
	}
}
